package com.lanyou.esb.repository;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;

import com.lanyou.esb.entity.IfAvgResTimes;

/**
 * 
 * @author Davey.wu
 */
public interface IfAvgResTimesRepository extends
		Repository<IfAvgResTimes, String>,
		JpaSpecificationExecutor<IfAvgResTimes> {

	@Query(nativeQuery = true, value = "SELECT t.IF_ID AS ifId, "
			+ "t.AVG_RES_TIMES AS tiems, " + "t.START_DATE AS startDate "
			+ "FROM t_esb_bu_if_avg_res_times t "
			+ "WHERE  " + "t.IF_ID=:ifId " + "AND t.START_DATE >=:startDate "
			+ "AND t.START_DATE <=:endDate ORDER BY t.START_DATE")
	List<Object[]> findByIfIdAndDate(@Param("ifId") String ifId,
			@Param("startDate") Date startDate,
			@Param("endDate") Date endDate);

	long count();

	boolean exists(String id);

	@RestResource(exported = true)
	<S extends IfAvgResTimes> S save(S entity); // could expose REST API

	<S extends IfAvgResTimes> List<S> save(Iterable<S> entities);

	@RestResource(exported = true)
	@RequiresPermissions(value = { "IfAvgResTimesManagement:view",
			"IfAvgResTimesManagement:maintain" }, logical = Logical.OR)
	IfAvgResTimes findOne(String id); // could expose REST API

	IfAvgResTimes getOne(String id);

	// 以下三个接口只能选一个暴露为REST API

	@RestResource(exported = false)
	List<IfAvgResTimes> findAll(); // could expose REST API

	@RestResource(exported = false)
	List<IfAvgResTimes> findAll(Sort sort); // could expose REST API

	@RestResource(exported = true)
	@RequiresPermissions(value = { "IfAvgResTimesManagement:view",
			"IfAvgResTimesManagement:maintain" }, logical = Logical.OR)
	Page<IfAvgResTimes> findAll(Pageable pageable); // could expose REST API

	List<IfAvgResTimes> findAll(Iterable<String> ids);

	// 以下两个接口，要么都暴露为REST API，要么都不暴露
	@RestResource(exported = true)
	// @RequiresPermissions(value={"IfAvgResTimesManagement:maintain"})
	void delete(String id); // could expose REST API

	@RestResource(exported = true)
	// @RequiresPermissions(value={"IfAvgResTimesManagement:maintain"})
	void delete(IfAvgResTimes entity); // could expose REST API

	void delete(Iterable<? extends IfAvgResTimes> entities);

	void deleteAll();

	void flush();

	<S extends IfAvgResTimes> S saveAndFlush(S entity);

	void deleteInBatch(Iterable<IfAvgResTimes> entities);

	void deleteAllInBatch();

}
